<template>
  <div>
    <h1>新建英雄</h1>
    <el-form ref="form" :model="form" label-width="80px">
      <el-tabs value="basic" type="border-card">
        <el-tab-pane label="基础信息" name="basic">
          <el-form-item label="英雄名称">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="英雄称号">
            <el-input v-model="form.title"></el-input>
          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="form.categories" multiple>
              <el-option
                v-for="item of categories"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="难度">
            <el-rate
              style="margin-top: 0.6rem"
              :max="9"
              show-score
              v-model="form.scores.difficult"
            ></el-rate>
          </el-form-item>
          <el-form-item label="技能">
            <el-rate
              style="margin-top: 0.6rem"
              :max="9"
              show-score
              v-model="form.scores.skills"
            ></el-rate>
          </el-form-item>
          <el-form-item label="攻击">
            <el-rate
              style="margin-top: 0.6rem"
              :max="9"
              show-score
              v-model="form.scores.attack"
            ></el-rate>
          </el-form-item>
          <el-form-item label="生存">
            <el-rate
              style="margin-top: 0.6rem"
              :max="9"
              show-score
              v-model="form.scores.survive"
            ></el-rate>
          </el-form-item>

          <el-form-item label="顺风出装">
            <el-select v-model="form.items1" multiple>
              <el-option
                v-for="item of items"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="逆风出装">
            <el-select v-model="form.items2" multiple>
              <el-option
                v-for="item of items"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="英雄头像">
            <el-upload
              class="avatar-uploader"
              :action="$http.defaults.baseURL + '/upload'"
              :headers="getAuthHeaders()"
              :show-file-list="false"
              :on-success="afterUpload"
            >
              <img v-if="form.avatar" :src="form.avatar" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="Banner">
            <el-upload
              class="avatar-uploader"
              :action="uploadUrl"
              :headers="getAuthHeaders()"
              :show-file-list="false"
              :on-success="res => $set(form, 'banner', res.url)"
            >
              <img v-if="form.banner" :src="form.banner" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="英雄称号">
            <el-input v-model="form.title"></el-input>
          </el-form-item>
          <el-form-item label="使用技巧">
            <el-input type="textarea" v-model="form.usageTips"></el-input>
          </el-form-item>
          <el-form-item label="对抗技巧">
            <el-input type="textarea" v-model="form.battleTips"></el-input>
          </el-form-item>
          <el-form-item label="团战思路">
            <el-input type="textarea" v-model="form.teamTips"></el-input>
          </el-form-item>
          <el-button @click="save()">创建</el-button>
        </el-tab-pane>

        <!-- tab2 -->
        <el-tab-pane label="技能" name="skills">
          <el-button size="small" @click="form.skills.push({})">
            <i class="el-icon-plus"></i> 添加英雄技能
          </el-button>
          <el-row type="flex" style="flex-wrap: wrap">
            <!-- 原本是空数组 通过上面@click="form.skills.push(push({}))添加技能展示" -->
            <el-col :md="12" v-for="(item, i) in form.skills" :key="i">
              <el-form-item label="名称">
                <el-input v-model="item.name"></el-input>
              </el-form-item>
              <el-form-item label="技能头像">
                <el-upload
                  class="avatar-uploader"
                  :action="$http.defaults.baseURL + '/upload'"
                  :headers="getAuthHeaders()"
                  :show-file-list="false"
                  :on-success="res => $set(item, 'icon', res.url)"
                >
                  <img v-if="item.icon" :src="item.icon" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="冷却值">
                <el-input v-model="item.delay"></el-input>
              </el-form-item>
              <el-form-item label="消耗">
                <el-input v-model="item.cost"></el-input>
              </el-form-item>
              <el-form-item label="描述">
                <el-input v-model="item.description" type="textarea"></el-input>
              </el-form-item>
              <el-form-item label="小提示">
                <el-input v-model="item.tips" type="textarea"></el-input>
              </el-form-item>

              <!-- 删除英雄一列  splice(i,1)第一个参数是指定要删除的位置  第二个参数是要删几个-->
              <el-form-item>
                <el-button size="small" type="danger" @click="form.skills.splice(i, 1)">删除</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>


        <!-- tab3 -->
        <el-tab-pane label="最佳搭档" name="partners">
              <el-button size="small" @click="form.partners.push({})">
            <i class="el-icon-plus"></i> 最佳搭档
          </el-button>
            <el-row type="flex" style="flex-wrap: wrap">
            <el-col :md="12" v-for="(item, i) in form.partners" :key="i">
              <el-form-item label="英雄">
                <el-select filterable v-model="item.hero">
                  <el-option 
                  v-for="hero in heroes"
                  :key="hero._id"
                  :value="hero._id"
                  :label="hero.name"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="描述">
                <el-input v-model="item.description" type="textarea"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
             </el-tab-pane>
      </el-tabs>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        name: "",
        avatar: "",
        banner:"",
        scores: {},
        skills: [],
        partners: [],
      },
      // 父级的选项
      parents: [],
      //   分类列表
      categories: [],
      //   物品列表
      items: [],
      //   英雄列表
      heroes: [],
    };
  },
  created() {
    this.getList();
    this.fetchItems();
    this.fetchHeroes();
  },
  methods: {
    //   提交上传
    async save() {
      const res = await this.$http.post("/rest/heroes", this.form);
      console.log(res);
    },

    // 关联的是获取新建分类列表
    async getList() {
      const res = await this.$http.get(`rest/categories`);
      this.categories = res.data;
    },

    //关联的是已经建好的物品列表
    async fetchItems() {
      const res = await this.$http.get(`rest/items`);
      this.items = res.data;
    },
    // 这里是已经建好的英雄 添加英雄技能的时候要恋情
    async fetchHeroes() {
      const res = await this.$http.get(`rest/heroes`);
      this.heroes = res.data;
    },

    afterUpload(res) {
      console.log(res);
      // 显示赋值 vue提供的 明确告诉vue 我们要在form上添加一个icon属性
      this.$set(this.form, "icon", res.url);
      this.form.avatar = res.url;
    },
  },
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>